package com.hexiang.spock

import cn.hutool.core.text.CharSequenceUtil
import cn.hutool.core.util.StrUtil
import org.apache.commons.lang3.StringUtils
import org.junit.runner.RunWith
import org.mockito.Mockito
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PowerMockIgnore
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import org.powermock.modules.junit4.PowerMockRunnerDelegate
import org.spockframework.runtime.Sputnik
import spock.lang.Specification
import spock.lang.Unroll

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Sputnik.class)
@PrepareForTest([Account.class, StrUtil.class, CharSequenceUtil.class, StringUtils.class])
@PowerMockIgnore(["org.w3c.dom.*", "javax.xml.*", "org.xml.*", "javax.management.*"])
class CustomerTest extends Specification {

    void setup() {
        PowerMockito.mockStatic(StrUtil.class);
        PowerMockito.mockStatic(CharSequenceUtil.class);
        PowerMockito.mockStatic(StringUtils.class);
    }

    @Unroll
    def "GetAccountType"() {
        setup:
        PowerMockito.mockStatic(Account.class);
        PowerMockito.when(Account.getType()).thenReturn("bb")
        Customer cust = new Customer();

        when:
        String ret = cust.getAccountType()

        then:
        ret == "bb"
    }

    @Unroll
    def "static"() {
        setup:
//        PowerMockito.when(StringUtils.isEmpty(Mockito.anyString())).thenReturn(false);
        PowerMockito.when(StrUtil.isEmpty(Mockito.anyString())).thenReturn(false);
        Customer cust = new Customer();

        when:
        boolean ret = cust.test(param)

        then:
        ret == result

        where: "验证分支场景"
        param | result
        "parent1"   | false
    }

    @Unroll
    def "static1"() {
        setup:
        PowerMockito.when(StrUtil.isEmpty(Mockito.anyString())).thenReturn(false);

        when:
        boolean ret = StrUtil.isEmpty(param)

        then:
        ret == result

        where:
        param | result
        "str"   | false
    }
}